package org.mili.ticketeer.service;

import org.mili.ticketeer.dao.DaoFactory;
import org.mili.ticketeer.dao.ModificationResult;
import org.mili.ticketeer.dao.RoleDao;
import org.mili.ticketeer.dto.Contact;
import org.mili.ticketeer.dto.ContactRole;
import org.mili.ticketeer.dto.Role;
import org.mili.ticketeer.util.Lambda;

import java.util.List;

public class RoleService extends Service {
	private RoleDao _roleDao = DaoFactory.getDao(RoleDao.class);

    public List<Role> getRoles() throws ServiceException {
        return doReadInService(new Lambda<List<Role>>() {
            @Override
            public List<Role> exec(Object... params) throws Exception {
                return _roleDao.getRoles();
            }
        });
    }

    public ModificationResult write(final Role role) throws ServiceException {
        return doInService(new Lambda<ModificationResult>() {
            @Override
            public ModificationResult exec(Object... params) throws Exception {
                return _roleDao.write(role);
            }
        });
    }

    public Boolean assign(final Contact contact, final Role role) throws ServiceException {
        return doInService(new Lambda<Boolean>() {
            @Override
            public Boolean exec(Object... params) throws Exception {
                return _roleDao.assign(contact, role);
            }
        });
    }

    public Boolean unassign(final Contact contact, final Role role) throws ServiceException {
        return doInService(new Lambda<Boolean>() {
            @Override
            public Boolean exec(Object... params) throws Exception {
                return _roleDao.unassign(contact, role);
            }
        });
    }

    public Role findRoleByName(final String name) throws ServiceException {
        return doReadInService(new Lambda<Role>() {
            @Override
            public Role exec(Object... params) throws Exception {
                return _roleDao.findByName(name);
            }
        });
    }

    public Role findRoleById(final int id) throws ServiceException {
        return doReadInService(new Lambda<Role>() {
            @Override
            public Role exec(Object... params) throws Exception {
                return _roleDao.findById(id);
            }
        });
    }

    public ModificationResult delete(final Role role) throws ServiceException {
        return doInService(new Lambda<ModificationResult>() {
            @Override
            public ModificationResult exec(Object... params) throws Exception {
                return _roleDao.delete(role);
            }
        });
    }

    public List<ContactRole> getContactRolesFor(final Contact contact) throws ServiceException {
        return doReadInService(new Lambda<List<ContactRole>>() {
            @Override
            public List<ContactRole> exec(Object... params) throws Exception {
                return _roleDao.getContactRolesFor(contact);
            }
        });
    }

    public List<ContactRole> getAssignedContactRolesFor(final Contact contact) throws ServiceException {
        return doReadInService(new Lambda<List<ContactRole>>() {
            @Override
            public List<ContactRole> exec(Object... params) throws Exception {
                return _roleDao.getAssignedContactRolesFor(contact);
            }
        });
    }

    public List<ContactRole> getContactRoles() throws ServiceException {
        return doReadInService(new Lambda<List<ContactRole>>() {
            @Override
            public List<ContactRole> exec(Object... params) throws Exception {
                return _roleDao.getContactRoles();
            }
        });
    }

}
